package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.UserManagement;
import com.itheima.service.UserManagementService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/*
用户管理
CREATE TABLE `t_usermangement_role` (
  `usermangement_id` INT(11) NOT NULL,
  `role_id` INT(11) NOT NULL,
  PRIMARY KEY (`usermangement_id`,`role_id`),
  KEY `FK_Reference_90` (`role_id`),
  CONSTRAINT `FK_Reference_70` FOREIGN KEY (`usermangement_id`) REFERENCES `usermangement` (`id`),
  CONSTRAINT `FK_Reference_90` FOREIGN KEY (`role_id`) REFERENCES `t_role` (`id`)
)


CREATE TABLE `usermangement` (
  `id` INT(12) NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(32) DEFAULT NULL,
  `sex` VARCHAR(12) DEFAULT NULL,
  `birthday` VARCHAR(32) DEFAULT NULL,
  `telephone` VARCHAR(32) DEFAULT NULL,
  `staus` VARCHAR(32) DEFAULT NULL,
  `remark` VARCHAR(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
)
 */
@RestController
@RequestMapping("/usermanagement")
public class UserManagementController {
    @Reference
    private UserManagementService userManagementService;
    //新增用户
    @RequestMapping("/add")
    public Result add(@RequestBody UserManagement userManagement,Integer[]roleIds,String staus,String sex){//创建usermanagement类建表
        try {
            userManagementService.add(userManagement,roleIds,staus,sex);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }
        return new Result(true,MessageConstant.ADD_USER_SUCCESS);
    }
    //用户分页查询
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = userManagementService.pageQuery(queryPageBean);
        return pageResult;
    }


    //编辑
    @RequestMapping("edit")
    public Result edit(@RequestBody UserManagement userManagement,Integer[] roleIds,String staus,String sex){
        try {
            userManagementService.edit(userManagement,roleIds,staus,sex);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_USER_FAIL);
        }
            return new Result(true,MessageConstant.EDIT_USER_SUCCESS);
    }


    //根据id查询用户
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            UserManagement userManagement = userManagementService.findById(id);
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS, userManagement);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
    }
        //根据角色id查询用户包含的多个角色id
        @RequestMapping("/findRoleIdsByUserManagementId")
        public Result findRoleIdsByUserManagementId (Integer id){
            try {
                List<Integer> roleIds = userManagementService.findRoleIdsByUserManagementId(id);
                return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, roleIds);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
            }
        }

    //删除用户
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            userManagementService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    //查询所有的用户
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<UserManagement> list = userManagementService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

}
